package com.yunkaweilai.android.view.popup;

import android.app.Activity;
import android.support.annotation.IdRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.interfaces.PopupShopEmployee;
import com.yunkaweilai.android.model.jici.JiciValidityModel;
import com.yunkaweilai.android.model.operation.CreateSelectedGoodsModel;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;

import java.util.Calendar;
import java.util.TimeZone;

import cn.addapp.pickers.picker.DatePicker;
import razerdp.basepopup.BasePopupWindow;

/**
 * 计次有效期设置
 * Created by Administrator on 2017/7/17.
 */

public class JiciValidityPopup extends BasePopupWindow{

    private View popupView;
    private Activity mContext;
    private ListView idListView;
    private TextView idTvRight;
    private PopupShopEmployee popupShopEmployee;
    private RadioGroup idGroup;
    private TextView idTvType1;
    private LinearLayout idLlayoutType2;
    private LinearLayout idLlayoutType3;
    private EditText idEdtDay;
    private TextView idTvSel1,idTvSel2;
    private CreateSelectedGoodsModel.DataBean.GoodsListBean item;
    private String validity_type = "permanent";
    private String jiciStr = "";
    private String id;
    private String good_type;
    private int mYear;
    private int mMonth;
    private int mDay;
    private View type2View;
    private Calendar c;
    public JiciValidityPopup(Activity context, String id,String good_type,CreateSelectedGoodsModel.DataBean.GoodsListBean item) {
        super(context);
        this.id = id;
        this.mContext = context;
        this.item = item;
        this.good_type = good_type;
        bindEvent();
    }
    public void setInterface(PopupShopEmployee popupShopEmployee){
        this.popupShopEmployee = popupShopEmployee;
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(250 * 2, 0, 300);
    }

    @Override
    public View getClickToDismissView() {
        return popupView.findViewById(R.id.id_img_close);
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.popup_jici_validity, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.id_rlayout_bottom);
    }

    private void bindEvent() {

        if (popupView != null) {
            initDate();
            initView();
            initClick();
        }
    }
    private void initDate() {
        c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = c.get(Calendar.YEAR);// 获取当前年份
        mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份
        mDay = c.get(Calendar.DAY_OF_MONTH);// 获取当前月份的日期号码
    }

    private void initClick() {
        idGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.id_rbtn_1:
                        idTvType1.setVisibility(View.VISIBLE);
                        idLlayoutType2.setVisibility(View.GONE);
                        idLlayoutType3.setVisibility(View.GONE);
                        type2View.setVisibility(View.GONE);
                        validity_type = "permanent";
                        break;
                    case R.id.id_rbtn_2:
                        idTvType1.setVisibility(View.GONE);
                        idLlayoutType2.setVisibility(View.VISIBLE);
                        type2View.setVisibility(View.VISIBLE);
                        idLlayoutType3.setVisibility(View.GONE);
                        validity_type = "day";
                        break;
                    case R.id.id_rbtn_3:
                        idTvType1.setVisibility(View.GONE);
                        type2View.setVisibility(View.GONE);
                        idLlayoutType2.setVisibility(View.GONE);
                        idLlayoutType3.setVisibility(View.VISIBLE);
                        validity_type = "appoint";
                        break;
                }
            }
        });
        idTvSel1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimePicker(idTvSel1);
            }
        });
        idTvSel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimePicker(idTvSel2);
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                JiciValidityModel jiciValidityModel = new JiciValidityModel();
                if ("appoint".equals(validity_type)){
                    if ("请选择".equals(idTvSel1.getText().toString()) || "请选择".equals(idTvSel2.getText().toString()) ){
                        MyToast.showShortToast(mContext,"还未选择日期");
                        return;
                    }else{
                        jiciStr = idTvSel1.getText().toString() + "至" + idTvSel2.getText().toString();
                        jiciValidityModel.setStart_time(idTvSel1.getText().toString());
                        jiciValidityModel.setEnd_time(idTvSel2.getText().toString());
                        jiciValidityModel.setIs_permanent("0");
                    }
                }else if ("day".equals(validity_type)){
                    if (StringUtils.isEmpty(idEdtDay.getText().toString())){
                        MyToast.showShortToast(mContext,"还未填写天数");
                        return;
                    }else{
                        jiciValidityModel.setValidity_day(idEdtDay.getText().toString());
                        jiciValidityModel.setIs_permanent("0");
                    }
                    jiciStr = "至" +  Utils.datePlus(Integer.parseInt(idEdtDay.getText().toString()));

                }else{
                    jiciStr = idTvType1.getText().toString();
                    jiciValidityModel.setIs_permanent("1");
                }
                jiciValidityModel.setGoods_id(id);
                jiciValidityModel.setGoods_type(good_type);

                jiciValidityModel.setValidity_type(validity_type);
                item.setJiciValidityModel(jiciValidityModel);
                item.setValidityStr(jiciStr);
                popupShopEmployee.refresh();
                dismiss();
            }
        });
    }

    private void initView() {
        idTvRight = ((TextView) popupView.findViewById(R.id.id_tv_right));
        idGroup = ((RadioGroup) popupView.findViewById(R.id.id_group));
        idTvType1 = ((TextView) popupView.findViewById(R.id.id_tv_type1));
        idLlayoutType2 = ((LinearLayout) popupView.findViewById(R.id.id_llayout_type2));
        idEdtDay = ((EditText) popupView.findViewById(R.id.id_edt_day));
        idLlayoutType3 = ((LinearLayout) popupView.findViewById(R.id.id_llayout_type3));
        idTvSel1 = ((TextView) popupView.findViewById(R.id.id_tv_sel1));
        idTvSel2 = ((TextView) popupView.findViewById(R.id.id_tv_sel2));
        type2View = popupView.findViewById(R.id.id_type2_view);
    }


    private void showTimePicker(final TextView textView){
        final DatePicker picker = new DatePicker(mContext);
        picker.setCanLoop(false);
        picker.setWheelModeEnable(true);
        picker.setTopPadding(15);
        picker.setRangeStart(mYear, mMonth, mDay);
        picker.setRangeEnd(mYear + 100 , 11, 11);
        picker.setWeightEnable(true);
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                textView.setText(year + "-" + month + "-" + day);
            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {
            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();
    }
}
